<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\Admins;
use App\Models\V1\Bank;
use App\Models\V1\Config;
use App\Models\V1\Games;
use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use App\Models\V1\Users;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AuditController extends BaseController
{

    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAuditList(Request $request)
    {

//        $query = Bank::query();
        $query = Users::with(['Community','Preparatory']);
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');
        if ($request->filled('username')) {
            $query->where('username', 'like', '%' . $request->input('username') . '%');
        }
        if ($request->filled('mobile')) {
            $query->where('mobile', "like", "%" . $request->input('mobile') . "%");
        }

        if ($request->filled('id')) {
            $query->where('id', $request->input('id'));
        }


        if ($request->filled('community_id')) {
            $query->where('community_id', $request->input('community_id'));
        }

        if ($request->filled('preparatory_id')) {
            $query->where('preparatory_id', $request->input('preparatory_id'));
        }



        if ($request->filled('state')) {
            $query->where('state', $request->input('state'));
        }
        $query->orderBy($sort, $order);
//        var_dump($query->toSql());
        $data = $query->paginate($pageSize);


//        $bank_model = new Bank();
//        foreach ($data->items() as &$value) {
//            $value->bank_info = $bank_model->where('users_id', $value->id)->first();
//        }
        return ajax_output(200, "成功", $data);
    }


    /**
     * 改变用户转态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeUserBankStatus(Request $request)
    {
        $users_id = $request->input('id', 0);
        $users_model = new Users();

        $vo = $users_model->find($users_id);
        if (!$vo) {
            return ajax_output(300, "数据不存在");
        }

        $state = $vo->state = (int)$request->input('status', 1);


        $vo->save();

        $bank = Bank::where('users_id', $users_id)->first();
        if ($bank) {
            Bank::where('users_id', $users_id)->update([
                'state' => $state,
            ]);
        }

        return ajax_output(200, "成功");


//        $bank_model = new Bank();
//        $bank = $bank_model->where('users_id',$users_id)->first();
//        $vo = $bank_model->find($bank['id']);
//        if (!$vo) {
//            return ajax_output(300, "数据不存在");
//        }
//        $vo->state = (int)$request->input('status', 1);
//        $vo->save();
//        return ajax_output(200, "成功");
    }


    /**
     * 驳回用户个人认证
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function submitAudit(Request $request)
    {
        $id = $request->input('bankId', 0);//用户id
        $type = $request->input('type', 0);//0驳回 1通过

        try {
            DB::beginTransaction();
            $user_model = new Users();
            $vo_u = $user_model->find($id);
            if (!$vo_u) {
                return ajax_output(300, "数据不存在");
            }
            if ($type == 1) {
                $vo_u->audit_status = (int)$request->input('audit_status', 3);
                $vo_u->dev_class = 'P1';
                $settlement_ratio =  Config::query()->where('config_key','settlement_ratio')->value('config_value');
                $vo_u->settlement_ratio = $settlement_ratio;//结算比例


            } else {
                $vo_u->audit_status = (int)$request->input('audit_status', 4);
            }
            $vo_u->save();
            DB::commit();
            return ajax_output(200, "成功");

        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
    }


    /**
     * 用户详情
     * @param $id
     * @return void
     */
    public function getUserInfo($id)
    {
        $vo = Users::find($id);
        $user_model = new Users();
        $red = $user_model->where('id', $vo['users_id'])->get();
        $vo['info'] = $red;
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 修改用户
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function updateUser(Request $request, $id)
    {
        try {
            DB::beginTransaction();


            $real_name = $request->input('real_name');
            $mobile = $request->input('mobile');
            $open_bank = $request->input('open_bank');
            $card_number = $request->input('card_number');


            $qq = $request->input('qq');
            $weixin = $request->input('weixin');
            $auth_video = $request->input('auth_video');
            $identity_front = $request->input('identity_front');
            $identity_back = $request->input('identity_back');
            $settlement_ratio = $request->input('settlement_ratio');

            $data['real_name'] = $real_name;
            $data['mobile'] = $mobile;
            $data['open_bank'] = $open_bank;
            $data['card_number'] = $card_number;
            $data['auth_video'] = $auth_video;
            $data['identity_front'] = $identity_front;
            $data['identity_back'] = $identity_back;
            $data['settlement_ratio'] = $settlement_ratio;

            $data['qq'] = $qq;
            $data['weixin'] = $weixin;


            $voo = Users::find($id);
            $voo->update($data);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");

    }


}
